package org.jdesktop.wonderland.modules.printscreenplugin.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import org.jdesktop.wonderland.client.BaseClientPlugin;
import org.jdesktop.wonderland.client.cell.view.ViewCell;
import org.jdesktop.wonderland.client.hud.CompassLayout;
import org.jdesktop.wonderland.client.hud.HUD;
import org.jdesktop.wonderland.client.hud.HUDComponent;
import org.jdesktop.wonderland.client.hud.HUDEvent;
import org.jdesktop.wonderland.client.hud.HUDEventListener;
import org.jdesktop.wonderland.client.hud.HUDManagerFactory;
import org.jdesktop.wonderland.client.jme.JmeClientMain;
import org.jdesktop.wonderland.client.jme.ViewManager;
import org.jdesktop.wonderland.client.login.ServerSessionManager;
import org.jdesktop.wonderland.common.annotation.Plugin;

/**
 *
 * @author Dominik
 */
@Plugin
public class PrintScreenClientPlugin extends BaseClientPlugin {

    // The top map menu item to add to the Windows menu
    private JMenuItem printscreenItem = null;
    // The HUD Component displaying the top map.
    private HUDComponent hudComponent = null;
    // Listener for changes in the primary view Cell
    private ViewManager.ViewManagerListener viewManagerListener = null;
    //For Communication with server session
    private ServerSessionManager serverSession;
    //KeyEvent Listener for PrintScreen
    private PrintScreenListener printscreenKeyListener;
    private static final Logger LOGGER =
            Logger.getLogger(JmeClientMain.class.getName());
    
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void initialize(ServerSessionManager loginInfo) {
        
        printscreenKeyListener = new PrintScreenListener(loginInfo);  
        //printscreenKeyListener.createInfoPanel();
        JmeClientMain.getFrame().getFrame().addKeyListener(printscreenKeyListener);
        JmeClientMain.getFrame().getCanvas3DPanel().addKeyListener(printscreenKeyListener);
        JmeClientMain.getFrame().getCanvas().addKeyListener(printscreenKeyListener);
        LOGGER.log(Level.INFO, "KeyListener Added");
        
        serverSession = loginInfo;
        // Create the Top Map menu item.
        printscreenItem = new JCheckBoxMenuItem("Printscreen Folder");
        printscreenItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Depending upon whether the checkbox is selected or not,
                // either hide or show the HUD Component
                if (printscreenItem.isSelected() == true) {
                    if (hudComponent == null) {
                        hudComponent = createHUDComponent(serverSession);
                    }
                    hudComponent.setVisible(true);
                }
                else {
                    hudComponent.setVisible(false);
                }
            }
        });
        
        // Wait until we get a "primary view" Cell. We need one before we can
        // display the map.
        viewManagerListener = new PrintScreenViewManagerListener();
        ViewManager.getViewManager().addViewManagerListener(viewManagerListener);
        
        super.initialize(loginInfo);
    }
    
     /**
     * Creates and returns the top map HUD component.
     */
    private HUDComponent createHUDComponent(ServerSessionManager serverSession) {

        // Create the HUD Panel that displays the map.
        HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
        PrintScreenSaveFolderDialog panel = new PrintScreenSaveFolderDialog(printscreenKeyListener.getPrintScreenSaver());
        hudComponent = mainHUD.createComponent(panel);
        hudComponent.setName("Printscreen Folder");
        hudComponent.setPreferredLocation(CompassLayout.Layout.CENTER);
        mainHUD.addComponent(hudComponent);

        // Track when the HUD Component is closed. We need to update the state
        // of the check box menu item too. We also need to turn off the camera.
        hudComponent.addEventListener(new HUDEventListener() {
            public void HUDObjectChanged(HUDEvent event) {
                if (event.getEventType() == HUDEvent.HUDEventType.CLOSED) {
                    printscreenItem.setSelected(false);
                }
            }
        });
        
        return hudComponent;
    }  
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void cleanup() {
        // If there is a HUD Component, then remove it from the HUD and clean
        // it up. This really should happen when the primary view cell is
        // disconnected, but we do this here just in case.
        if (hudComponent != null) {
            HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
            mainHUD.removeComponent(hudComponent);
        }
        
    }
    
        /**
     * Listener for the changes in the primary view cell.
     */
    private class PrintScreenViewManagerListener implements ViewManager.ViewManagerListener {
        /**
         * {@inheritDoc}
         */
        public void primaryViewCellChanged(ViewCell oldCell, ViewCell newCell) {
            
            // If there is an old Cell, then remove the old HUD component and
            // the main menu item.
            if (oldCell != null) {
                // If there is a HUD Component, then remove it from the HUD and
                // clean it up. This really should happen when the primary view
                // cell is disconnected, but we do this here just in case.
                if (hudComponent != null) {
                    HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
                    mainHUD.removeComponent(hudComponent);
                }

                // Remove the menu item
                JmeClientMain.getFrame().removeFromWindowMenu(printscreenItem);
            }

            // If there is a new Cell, the add the menu item
            if (newCell != null) {
                JmeClientMain.getFrame().addToWindowMenu(printscreenItem, -1);
            }
        }
    }
}
